package nancal.mp.service.jenkins.publish

import nancal.mp.db.mongo.PipelineTemplateEnum
import nancal.mp.service.jenkins.JenkinsPublishJob
import nbcp.base.utils.UrlUtil


fun JenkinsPublishJob.f4_deploy_k8s(): FunctionContent {
    var logFileName = "curl-result.txt"


    if (this.appDeploySetting.pipelineTemplate == PipelineTemplateEnum.Native) {
        return FunctionContent.empty("deploy-k8s");
    }

    return FunctionContent(
        "deploy-k8s", """
echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` "部署镜像:"

echo "curl -X POST -o ${logFileName} -s -w %{http_code} --max-time 6 --retry 3 ${appDeploySetting.mpIntegrationApiUrl}/ops/k8s/deployment/deploy-with-log/${this.task.id}?admin-token=${UrlUtil.encodeURIComponent(appDeploySetting.adminToken)}"

code=＄(curl -X POST -o ${logFileName} -s -w  %{http_code} --max-time 6 --retry 3 ${appDeploySetting.mpIntegrationApiUrl}/ops/k8s/deployment/deploy-with-log/${this.task.id}?admin-token=${UrlUtil.encodeURIComponent(appDeploySetting.adminToken)})

if [ "＄code" == "000" ]; then
    echo "网络错误，请重试！"
    exit 3;
elif [ "＄code" == "200" ]  &&  grep -q '"code":0' ${logFileName} ; then
    echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` '部署镜像成功!'
    show-file ${logFileName} 50
else
    echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` '部署镜像失败!!! 返回: ＄code'
    show-file ${logFileName} 100
    exit 1
fi
"""
    )
}

fun JenkinsPublishJob.f4_update_nginx_conf(): FunctionContent {
    if (this.task.language.isNeedNginx() == false) {
        return FunctionContent.empty("update-nginx-conf");
    }

    if (this.appDeploySetting.pipelineTemplate == PipelineTemplateEnum.Native) {
        return FunctionContent.empty("update-nginx-conf");
    }

    var logFileName = "curl-result.txt"


    return FunctionContent(
        "update-nginx-conf", """
echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` "更新 k8s nginx configmap 虚拟目录配置:"

echo "curl -X POST -o ${logFileName} -H\"Content-type:application/json\" -s -w %{http_code} --max-time 30 --retry 3 ${appDeploySetting.mpIntegrationApiUrl}/ops/k8s/configmap/set-vue?admin-token=${UrlUtil.encodeURIComponent(appDeploySetting.adminToken)} -d" '{"targetEnv":"${this.appDeploySetting.code}","namespace":"${this.task.clusterCode}","location":"${this.task.buildInfo.location}","serviceName":"${this.task.buildInfo.serviceName}"}'
code=＄(curl -X POST -o ${logFileName} -H"Content-type:application/json" -s -w  %{http_code} --max-time 30 --retry 3 ${appDeploySetting.mpIntegrationApiUrl}/ops/k8s/configmap/set-vue?admin-token=${UrlUtil.encodeURIComponent(appDeploySetting.adminToken)}  -d '{"targetEnv":"${this.appDeploySetting.code}","namespace":"${this.task.clusterCode}","location":"${this.task.buildInfo.location}","serviceName":"${this.task.buildInfo.serviceName}"}')

if [ "＄code" == "000" ]; then
    echo "网络错误，请重试！"
    exit 3;
elif [ "＄code" == "200" ] &&  grep -q '"code":0' ${logFileName} ; then
    echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` '更新 configmap 成功! '
    show-file ${logFileName} 50
elif [ "＄code" == "234" ]; then
    echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` '更新 configmap 成功! '
    show-file ${logFileName} 50
    
    restart-nginx
else
    echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` '更新 configmap 失败!!! 返回: ＄code'
    show-file ${logFileName} 100
    exit 1
fi
"""
    )
}


fun JenkinsPublishJob.f4_restart_nginx(): FunctionContent {
    var logFileName = "curl-result.txt"


    return FunctionContent(
            "restart-nginx", """

echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` "重启 k8s nginx, namespace: ${this.task.clusterCode}"

echo "curl -X POST -o ${logFileName} -H\"Content-type:application/json\" -s -w %{http_code} --max-time 30 --retry 3 ${appDeploySetting.mpIntegrationApiUrl}/ops/k8s/deployment/restart-nginx?admin-token=${UrlUtil.encodeURIComponent(appDeploySetting.adminToken)} -d" '{"targetEnv":"${this.appDeploySetting.code}","namespace":"${this.task.clusterCode}"}'
code=＄(curl -X POST -o ${logFileName} -H"Content-type:application/json" -s -w  %{http_code} --max-time 30 --retry 3 ${appDeploySetting.mpIntegrationApiUrl}/ops/k8s/deployment/restart-nginx?admin-token=${UrlUtil.encodeURIComponent(appDeploySetting.adminToken)}  -d '{"targetEnv":"${this.appDeploySetting.code}","namespace":"${this.task.clusterCode}"}')

if [ "＄code" == "000" ]; then
    echo "网络错误，请重试！"
    exit 3;
elif [ "＄code" == "200" ] &&  grep -q '"code":0' ${logFileName} ; then
    echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` '重启 nginx 成功! '
    show-file ${logFileName} 50
else
    echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` '重启 nginx 失败!!! 返回: ＄code'
    show-file ${logFileName} 100
    exit 1
fi
"""
    )
}